import 'package:equatable/equatable.dart';
import 'package:excel_utils/pages/daily/daily_data_entry/bean/third_bean.dart';
import 'package:excel_utils/repository/daily_entry/daily_entry_repository.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

part 'daily_data_entry_state.dart';

class DailyDataEntryCubit extends Cubit<DailyDataEntryState> {
  DailyEntryRepository? dailyEntryRepository;
  DailyDataEntryCubit({this.dailyEntryRepository}) : super(DailyDataEntryInitial());

  createDateData({required List<ThirdBean> thirdData, required DateTime startDate, required DateTime endDate}) async {
    DateTime? startDateTemp = startDate;
    List<ThirdBean> thirdDataTemp = [];
    endDate = endDate.add(const Duration(days: 1));
    while (startDate.isBefore(endDate)) {
      var year = startDate.year;
      var month = startDate.month.toString().padLeft(2, '0');
      var day = startDate.day.toString().padLeft(2, '0');
      List<String> thirdDataKeys = [];
      for (var value in thirdData) {
        thirdDataKeys.add(value.createTime.toString());
      }
      if (thirdDataKeys.contains("$year-$month-$day")) {
        startDate = startDate.add(Duration(days: 1));
        thirdDataTemp.add(ThirdBean(createTime: "$year-$month-$day", createTimeStr: "$year-$month-$day", skDdc: "", skHvan: "", skBqsjBwb: ""));
        continue;
      }
      thirdDataTemp.add(ThirdBean(createTime: "$year-$month-$day", createTimeStr: "$year-$month-$day", skDdc: "", skHvan: "", skBqsjBwb: ""));
      startDate = startDate.add(Duration(days: 1));
    }
    thirdData.clear();
    thirdData.addAll(thirdDataTemp);
    // 根据日期获取服务端的数据
    String year1 = startDateTemp.year.toString();
    String month1 = startDateTemp.month.toString().padLeft(2, '0');
    String day1 = startDateTemp.day.toString().padLeft(2, '0');
    String startDateStr = "$year1-$month1-$day1";

    String year2 = endDate.year.toString();
    String month2 = endDate.month.toString().padLeft(2, '0');
    String day2 = endDate.day.toString().padLeft(2, '0');
    String endDateStr = "$year2-$month2-$day2";

    List<ThirdBean> thirds = await dailyEntryRepository?.findThirdByDateTime(startDateStr, endDateStr) ?? [];
    for (var value in thirds) {
      // 和 thirdData 的日期对比，如果存在，则更新数据，不存在则新增数据
      if (thirdData.any((element) => element.createTimeStr == value.createTimeStr)) {
        ThirdBean third = thirdData.firstWhere((element) => element.createTimeStr == value.createTimeStr);
        third.id = value.id;
        third.createTime = value.createTime;
        third.createTimeStr = value.createTimeStr;
        third.skDdc = value.skDdc;
        third.skHvan = value.skHvan;
        third.skBqsjBwb = value.skBqsjBwb;
      }
    }
    emit(DailyDataCreateDateState(timeStamp: DateTime.now(), thirdDatas: thirdData));
  }

  getDailyData({maps}) async {}

  saveSignData({required List<ThirdBean> thirdData, required ThirdBean thirdBean}) async {
    emit(DailyDataSaveSignLoadingState(key: thirdBean.createTimeStr));
    Map<String, dynamic> thirdDataMap = {};
    ThirdBean? third = await dailyEntryRepository?.saveThird(thirdBean);
    if (third != null) {
      // 和 thirdData 的日期对比，如果存在，则更新数据，不存在则新增数据
      if (thirdData.any((element) => element.createTimeStr == third.createTimeStr)) {
        ThirdBean thirdTemp = thirdData.firstWhere((element) => element.createTimeStr == third.createTimeStr);
        thirdTemp.id = third.id;
        thirdTemp.createTime = third.createTime;
        thirdTemp.createTimeStr = third.createTimeStr;
        thirdTemp.skDdc = third.skDdc;
        thirdTemp.skHvan = third.skHvan;
        thirdTemp.skBqsjBwb = third.skBqsjBwb;
      }
      emit(DailyDataEntrySignSuccessState(key: thirdBean.createTimeStr));
    } else {
      emit(DailyDataEntrySignErrorState(message: "保存失败", key: thirdBean.createTimeStr));
    }
  }

  saveAllData({required List<ThirdBean> thirdData}) async {
    emit(DailyDataSaveAllLoadingState());
    List<ThirdBean> thirds = await dailyEntryRepository?.saveAllThird(thirdData) ?? [];
    for (var value in thirds) {
      // 和 thirdData 的日期对比，如果存在，则更新数据，不存在则新增数据
      if (thirdData.any((element) => element.createTime == value.createTime)) {
        ThirdBean third = thirdData.firstWhere((element) => element.createTime == value.createTime);
        third.id = value.id;
        third.createTime = value.createTime;
        third.createTimeStr = value.createTimeStr;
        third.skDdc = value.skDdc;
        third.skHvan = value.skHvan;
        third.skBqsjBwb = value.skBqsjBwb;
      }
    }
    emit(DailyDataEntrySuccessState(thirdData: thirdData));
  }
}
